package com.aizhixin.lab.project.course.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.project.course.service.TeacherKnowledgePointService;
import com.aizhixin.lab.project.template.domain.KnowledgePointDomain;
import com.aizhixin.lab.project.template.service.KnowledgePointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/teacher/project/knowledgePoint")
@RestController
@Api(tags = "知识点", value = "知识点API")
public class TeacherKnowledgePointController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TeacherKnowledgePointService knowledgePointService;

    @RequestMapping(value = "/delete", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "删除知识点", httpMethod = "GET", notes = "删除知识点<br><br><b>lwq</b>")
    public ResponseEntity<?> findTaskInfo(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        try{
            result = knowledgePointService.delete(id);
//            result.put(ApiReturnConstants.SUCCESS, true);
//            result.put(ApiReturnConstants.DATA,taskInfo);
        }catch (Exception e){
            result.put(ApiReturnConstants.SUCCESS, false);
            result.put(ApiReturnConstants.ERROR,e.getMessage());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findKnowledgePoint", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询任务下所有知识点", httpMethod = "GET", notes = "查询任务下所有知识点<br><br><b>lwq</b>")
    public ResponseEntity<?> findKnowledgePoint(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }

            List<KnowledgePointDomain> knowledgePointDomain = knowledgePointService.findKnowledgePointDomain(id);
            result.put(ApiReturnConstants.SUCCESS, true);
            result.put(ApiReturnConstants.DATA,knowledgePointDomain);

        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/findKnowledgePointInfo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询知识点详情", httpMethod = "GET", notes = "查询知识点详情<br><br><b>lwq</b>")
    public ResponseEntity<?> findKnowledgePointInfo(@RequestHeader("Authorization") String token, @RequestParam String id) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (StringUtils.isEmpty(id)){
            result.put(ApiReturnConstants.SUCCESS,false);
            result.put(ApiReturnConstants.ERROR,"id不能为空");
            return new ResponseEntity(result, HttpStatus.OK);
        }
        result = knowledgePointService.findKnowledgePointInfo(id);
        return new ResponseEntity(result, HttpStatus.OK);
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "保存或编辑", httpMethod = "POST", notes = "保存或编辑(无id为新增)<br><br><b>lwq</b>")
    public ResponseEntity<?> save(@RequestHeader("Authorization") String token, @RequestBody KnowledgePointDomain domain) {
        AccountDTO account = accountService.getUserByToken(token);
        Map<String, Object> result = new HashMap<String, Object>();
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        if (domain.getMinutiaIds()!=null&&domain.getMinutiaIds().size()>0){
            for (Long minutiaId : domain.getMinutiaIds()) {
                KnowledgePointDomain knowledgePointDomain = new KnowledgePointDomain();
                BeanUtils.copyProperties(domain,knowledgePointDomain);
                knowledgePointDomain.setMinutiaId(minutiaId);
                String save = knowledgePointService.save(knowledgePointDomain);
            }
        }else{
            String save = knowledgePointService.save(domain);
        }
//            String save = knowledgePointService.save(domain);
            result.put(ApiReturnConstants.SUCCESS, true);
//            result.put(ApiReturnConstants.DATA,save);

        return new ResponseEntity(result, HttpStatus.OK);
    }

}
